package com.paschino.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.paschino.modal.Album;
import com.paschino.modal.StoreImage;

@Repository
public class ImageDAOImpl extends AbstractDAO implements ImageDAO {

	@Override
	public boolean addImage(StoreImage image) {
		getSession().save(image);
		if (image.getId() > 0) {
			return true;
		}
		return false;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<StoreImage> getListImage(){
		List<StoreImage> imageList = new ArrayList<StoreImage>();
		Query query = getSession().createQuery("from StoreImage s where (s.isDelete is null or s.isDelete = false)");
		imageList = query.list();
		return imageList;
	}
	
	@Override
	public StoreImage getImage(int imageId) {
		return (StoreImage) getSession().get(StoreImage.class, imageId);
	}

	@Override
	public boolean updateImage(StoreImage image) {
		String name = image.getName();
		getSession().update(image);
		if (name.equals(image.getName())) {
			return true;
		}
		return false;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<StoreImage> getListImageByAlbumID(Album album){
		List<StoreImage> imageList = new ArrayList<StoreImage>();
		Query query = getSession().createQuery("from StoreImage s where (s.isDelete is null or s.isDelete = false) and s.album = :album").setParameter("album", album);
		imageList = query.list();
		return imageList;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public StoreImage getImageByGroupBy(String imagename, String type){
		List<StoreImage> imageList = new ArrayList<StoreImage>();
		StoreImage storeImage = new StoreImage();
		Query query = getSession().createQuery("from StoreImage s where (s.isDelete is null or s.isDelete = false) and s.name = ? and s.imageType = ? GROUP BY s.imageType, s.name");
		query.setParameter(0, imagename);
		query.setParameter(1, type);
		imageList = query.list();
		if(imageList.size() == 1){
			for(int i = 0; i < imageList.size(); i++){
				storeImage = imageList.get(i);
			}
		}
		return storeImage;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<StoreImage> getListImageByIsShow(Album album){
		List<StoreImage> imageList = new ArrayList<StoreImage>();
		Query query = getSession().createQuery("from StoreImage s where (s.isDelete is null or s.isDelete = false) and s.album = :album and s.isShow = true").setParameter("album", album);
		imageList = query.list();
		return imageList;
	}
	
	@Override
	public int deleteImage(int imageID){
		Query query = getSession().createQuery("update StoreImage s set s.isDelete = true where s.id = :imageID");
		query.setParameter("imageID", imageID);
		int result = query.executeUpdate();
		return result;
	}

	@Override
	public int hideImage(int imageID){
		Query query = getSession().createQuery("update StoreImage s set s.isShow = false where s.id = :imageID");
		query.setParameter("imageID", imageID);
		int result = query.executeUpdate();
		return result;
	}
	
	@Override
	public int unhideImage(int imageID){
		Query query = getSession().createQuery("update StoreImage s set s.isShow = true where s.id = :imageID");
		query.setParameter("imageID", imageID);
		int result = query.executeUpdate();
		return result;
	}

}
